package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.MenuPathDao;
import com.xtedu.examproject.entity.MenuPath;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MenuPathDaoImpl implements MenuPathDao {

    @Override
    public int insert(MenuPath menuPath) throws Exception {
        return 0;
    }

    @Override
    public int update(MenuPath menuPath) throws Exception {
        return 0;
    }

    @Override
    public int delete(MenuPath menuPath) throws Exception {
        return 0;
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public MenuPath findById(int id) throws Exception {
        return null;
    }

    @Override
    public List<MenuPath> findAll() throws Exception {
        String sql = "select * from menu_paths";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<MenuPath> menuPaths = new ArrayList<>();
            while (rs.next()) {
                MenuPath menuPath = new MenuPath();
                menuPath.setId(rs.getInt("id"));
                menuPath.setPath(rs.getString("path"));
                menuPath.setDescription(rs.getString("description"));
                menuPaths.add(menuPath);
            }
            return menuPaths;
        }
    }

    @Override
    public PageHelper findAll(int pageindex, int pagesize) throws Exception {
        String sql = "select * from menu_paths limit ?,?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, (pageindex - 1) * pagesize);
            ps.setInt(2, pagesize);
            ResultSet rs = ps.executeQuery();
            List<MenuPath> menuPaths = new ArrayList<>();
            while (rs.next()) {
                MenuPath menuPath = new MenuPath();
                menuPath.setId(rs.getInt("id"));
                menuPath.setPath(rs.getString("path"));
                menuPath.setDescription(rs.getString("description"));
                menuPaths.add(menuPath);
            }
            return new PageHelper(menuPaths, pageindex, pagesize, menuPaths.size(), "menu_paths", pageindex);
        }
    }

    @Override
    public PageHelper findByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }

    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }
}
